/*
 * ValueObjectFactory.java
 * version v1.01
 */

package fr.mywiki.model.jdbcAdapter.values;

import java.sql.SQLException;

import fr.mywiki.model.jdbcAdapter.utils.DatabaseRecordFacade;


/**
 * The ValueObject object factory deals with creating and loading instances of ValueObject objects. Works
 * as a singleton.
 * 
 * @author Thomas
 */

public class ValueObjectFactory {
	/**
	 * The instance for this factory.
	 */
	private static ValueObjectFactory instance;
	
	private ValueObjectFactory() {}
	
	public static ValueObjectFactory getInstance() {
		if(instance==null)
			instance = new ValueObjectFactory();
		return instance;
	}
	
	/**
	 * Loads a specific record from the database, table entry, whose identifier (title) is given
	 * in parameter.
	 * 
	 * @param identifier the title of the wanted entry
	 * @return a EntryValue object corresponding to the given identifier, null if it was not found.
	 */
	public EntryValue loadEntry(Object identifier) {
		EntryValue value = new EntryValue();
		try {
			Object[] array = DatabaseRecordFacade.loadRecord(EntryValue.TYPE, EntryValue.IDENTIFIER,
					identifier);
			
			/* A result set has been loaded. It is eventually empty... */
			if (array == null)
				return null;
			
			/* The first record of the result set is interpreted as the correct entry. */
			value.setId((Long)array[0]); // id
			value.setTitle((String)array[1]); // title
			value.setContent((String) array[2]); // content
			value.setInDatabase(true);
		} catch (SQLException e) {
			/* an exception occured, let's consider nothing was found */
			return null;
		}
		return value;
	}
	
	/**
	 * Creates a new entry. This entry is not yet in the database.
	 * 
	 * @return a EntryValue object whose attributes are all null.
	 */
	public EntryValue createEntry() {
		EntryValue value = new EntryValue();
		value.setInDatabase(false);
		return value;
	}
	
	/**
	 * Loads a specific record from the database, table user, whose identifier (login) is given
	 * in parameter.
	 * 
	 * @param identifier the login of the wanted user
	 * @return a UserValue object corresponding to the given identifier, null if it was not found.
	 */
	public UserValue loadUser(Object identifier) {
		UserValue value = new UserValue();
		try {
			Object[] array = DatabaseRecordFacade.loadRecord(UserValue.TYPE, UserValue.IDENTIFIER,
					identifier);
			
			/* A result set has been loaded. It is eventually empty... */
			if (array == null)
				return null;
			
			/* The first record of the result set is interpreted as the correct entry. */
			value.setId((Long) array[0]); // id
			value.setLogin((String) array[1]); // login
			value.setPassword((String) array[2]); // password
			value.setInDatabase(true);
		} catch (SQLException e) {
			/* an exception occured, let's consider nothing was found */
			return null;
		}
		return value;
	}
	
	/**
	 * Creates a new user. This user is not yet in the database.
	 * 
	 * @return a UserValue object whose attributes are all null.
	 */
	public UserValue createUser() {
		UserValue value = new UserValue();
		value.setInDatabase(false);
		return value;
	}
}
